package com.fable.biz.operate.impl;

import com.fable.biz.operate.CustomerService;
import com.fable.biz.operate.dto.CustomerDTO;
import com.fable.biz.operate.query.CustomerQueryCondition;
import com.fable.dal.mapper.ext.CustomerExtMapper;
import com.fable.dal.model.CustomerDO;
import com.fable.dal.model.CustomerQuery;
import com.fable.utils.CalendarUtil;
import com.fable.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * Created by weishuai on 18/4/7.
 */
@Service("customerService")
public class CustomerServiceImpl implements CustomerService {

    private static final Logger LOG = LoggerFactory.getLogger(CustomerServiceImpl.class);

    @Autowired
    CustomerExtMapper customerExtMapper;

    @Override
    public int addOrModifyCustomer(CustomerDTO customerDTO) {
        int count = 0;
        CustomerDO customerDO = customerExtMapper.selectByPrimaryKey(customerDTO.getId());
        if (customerDO != null) {
            BeanUtils.copyProperties(customerDTO, customerDO);
            customerDO.setGmtModified(new Date());
            count = customerExtMapper.updateByPrimaryKeySelective(customerDO);
        } else {
            customerDO = new CustomerDO();
            BeanUtils.copyProperties(customerDTO, customerDO);
            customerDO.setGmtCreate(new Date());
            customerDO.setGmtModified(new Date());
            count = customerExtMapper.insertSelective(customerDO);
        }
        return count;
    }

    @Override
    public CustomerDO getById(Integer id) {
        return customerExtMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<CustomerDTO> findCustomerList(CustomerQueryCondition condition) {
        try {

            CustomerQuery customerQuery = new CustomerQuery();
            CustomerQuery.Criteria criteria = customerQuery.createCriteria();
            if (condition != null) {
                if (StringUtil.isNotBlank(condition.getName())) {
                    criteria.andNameLike(condition.getName());
                }

                if (condition.getStatus() != null) {
                    criteria.andStatusEqualTo(condition.getStatus());
                }
                if (StringUtil.isNotBlank(condition.getStartTime())) {
                    String startTime = condition.getStartTime() + " 00:00:00";
                    Date time = CalendarUtil.parseString2Date(startTime, CalendarUtil.TIME_PATTERN);
                    criteria.andGmtCreateGreaterThanOrEqualTo(time);
                }
                if (StringUtil.isNotBlank(condition.getEndTime())) {
                    String endTime = condition.getEndTime() + " 23:59:59";
                    Date time = CalendarUtil.parseString2Date(endTime, CalendarUtil.TIME_PATTERN);
                    criteria.andGmtCreateLessThanOrEqualTo(time);
                }
                if (StringUtil.isNotBlank((condition.getTheirBy()))) {
                    criteria.andTheirByEqualTo(condition.getTheirBy());
                }
            }

            customerQuery.setOrderByClause("id desc");
            List<CustomerDO> doList = customerExtMapper.selectByQuery(customerQuery);
            List<CustomerDTO> dtoList = new ArrayList<>();

            doList.stream().filter(Objects::nonNull).forEach(customerDO -> {
                CustomerDTO customerDTO = new CustomerDTO();
                BeanUtils.copyProperties(customerDO, customerDTO);
                dtoList.add(customerDTO);
            });
            LOG.info("获取客户成功:{}", dtoList);
            return dtoList;
        } catch (Exception e) {
            LOG.info("获取客户失败:{}", e.getLocalizedMessage());
        }
        return null;
    }

    @Override
    public int delCustomer(CustomerDTO customerDTO) {
        int count = 0;
        try {
            if (customerDTO != null) {
                CustomerDO customerDO = new CustomerDO();
                BeanUtils.copyProperties(customerDTO, customerDO);
                count = customerExtMapper.updateByPrimaryKeySelective(customerDO);
            }
            LOG.info("逻辑删除客户成功:" + customerDTO.getId());
        } catch (Exception e) {
            LOG.error("逻辑删除客户失败:{}", e.getLocalizedMessage());
        }
        return count;
    }

    @Override
    public int priPriCustomer(String theirBy) {
        CustomerQuery customerQuery = new CustomerQuery();
        CustomerQuery.Criteria criteria = customerQuery.createCriteria();
        criteria.andTheirByEqualTo(theirBy);
        return customerExtMapper.selectByQuery(customerQuery).size();

    }
}
